//
//  NGHomeListClassifyCell.swift
//  NingguoForum
//
//  Created by zhengsw on 2022/11/9.
//  Copyright © 2022 zhengsw. All rights reserved.
//分类-归类-九宫格展示

import UIKit

class NGHomeListClassifyCell: UITableViewCell {

    var datas: NSArray = {
        let datas = NSArray()
        return datas
    }()
    
    let collectionView: UICollectionView!
    let layout: UICollectionViewFlowLayout!
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        layout = UICollectionViewFlowLayout()
        
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: KSCREEN_WIDTH, height: 150), collectionViewLayout: layout)
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .red

        let itemWidth = (KSCREEN_WIDTH - 100) / 5
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
        
        collectionView.delegate = self
        collectionView.dataSource = self;
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(NGHomeListClassifyCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        contentView.addSubview(collectionView)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setDatas(datas: NSArray){

        self.datas = datas

        collectionView.reloadData()
    }
    
}

extension NGHomeListClassifyCell: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NGHomeListClassifyCollectionViewCell
        let model = self.datas[indexPath.item] as! HomeClassifyModel
        cell.setModel(model: model)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}

